package com.atguigu;

import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import com.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.stream.Stream;

/**
 * @author hjy
 * @create 2023/3/7 18:23
 */
public class flink_cdc_demo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        System.setProperty("HADOOP_USER_NAME","atguigu");
        //设置检查点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        //检查点的时间，超出这个时间没完成就抛弃
        env.getCheckpointConfig().setCheckpointTimeout(60000L);
        //设置检查点的存储路径
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/flink-cdc");
        // 状态后端 ！！！
        env.setStateBackend(new HashMapStateBackend());

        MySqlSource<String> mySQLSource = MySqlSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .username("root")
                .password("123456")
                .databaseList("gmall")
                .tableList("gmall.base_trademark")
                //一定要记得反序列化
                .deserializer(new JsonDebeziumDeserializationSchema())
                .startupOptions(StartupOptions.initial())
                .build();
        DataStreamSource<String> dataStream = env.fromSource(mySQLSource, WatermarkStrategy.noWatermarks(), "mysql");

        dataStream.print();

        env.execute();
    }
}
